<?php

///////////////////////////////////////////////////////////////////////////////////////////////////
class XmobileLinksPluginAbstract extends XmobilePlugin
{
	function __construct( $mydirname )
	{
		XmobilePlugin::XmobilePlugin();

		$this->initVar('lid', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('cid', XOBJ_DTYPE_TXTBOX, '', true);
		$this->initVar('title', XOBJ_DTYPE_TXTBOX, '', true, 255);
		$this->initVar('url', XOBJ_DTYPE_TXTBOX, '', true, 255);
		$this->initVar('murl', XOBJ_DTYPE_TXTBOX, '', true, 255);
		$this->initVar('submitter', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('guestname', XOBJ_DTYPE_TXTBOX, '', true, 50);
		$this->initVar('status', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('date', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('hits', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('rating', XOBJ_DTYPE_FLOAT, '0', true);
		$this->initVar('votes', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('adult', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('ip', XOBJ_DTYPE_TXTBOX, '', true, 20);

		$this->setKeyFields(array('lid'));
		$this->setAutoIncrementField('lid');
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
class XmobileLinksPluginHandlerAbstract extends XmobilePluginHandler
{
	//var $mytrustdirname = 'links' ;

	var $item_id_fld    = 'lid';
	var $item_title_fld = 'title';
	var $item_date_fld  = 'date';
	var $item_uid_fld   = 'submitter';
	var $item_hits_fld  = 'hits';
	var $item_adult_fld = 'adult';
	var $item_murl_fld  = 'murl';
	var $item_url_fld   = 'url';

	////////////////////////////////////////////////////////////////////////
	function __construct($mydirname, $db)
	{
		global $xoopsConfig;

		XmobilePluginHandler::XmobilePluginHandler($db);
		if (! preg_match('/^[a-zA-Z0-9_-]+$/', $mydirname)) {
			trigger_error( 'invalid plugin name' , E_USER_ERROR ) ;
		}
		$this->moduleDir = $mydirname ;

		$this->itemTableName = $mydirname .'_links';
		$this->itemTextName = $mydirname .'_text';
		$this->db = $db ;
	}
	////////////////////////////////////////////////////////////////////////
	function setItemCriteria()
	{
		$this->item_criteria =& new CriteriaCompo();
		$this->item_criteria->add(new Criteria('status',0,'>'));
		$this->item_criteria->add(new Criteria('date',time(),'<'));
		if (isset($_GET['lid'])) {
			$this->item_criteria->add(new Criteria('lid', intval($_GET['lid'])));
		}
		$this->item_criteria->setSort('date');
		$this->item_criteria->setOrder('DESC');
	}

	function getItemDetail()
	{
		$this->utils->setDebugMessage(__CLASS__, 'getItemDetail criteria', $this->item_criteria->render());
		if (!$itemObjectArray =& $this->getObjects($this->item_criteria))
		{
			$this->utils->setDebugMessage(__CLASS__, 'getItemDetail Error', $this->getErrors());
		}

		if (count($itemObjectArray) == 0)
		{
			return false;
		}

		$itemObject = $itemObjectArray[0];

		if (!is_object($itemObject))
		{
			return false;
		}

		$this->item_id = $itemObject->getVar($this->item_id_fld);
		//$url_parameter = $this->getBaseUrl();
		//$itemObject->assignSanitizerElement();

		$detail4html = '';
		$detail4html .= _MD_XMOBILE_ITEM_DETAIL.'<br />';
		// TITLE
		if (!is_null($this->item_title_fld))
		{
			$title = $itemObject->getVar($this->item_title_fld) ;
			$t_len = 12 ;
			if ( function_exists('mb_strlen') && function_exists('mb_substr') ) {
				if( mb_strlen($title) > $t_len ){
					$title = mb_substr($title,0,$t_len)."..";
				}
			}elseif( strlen($title) > $t_len ){
				$title = substr($title,0,$t_len)."..";
			}
			$adult = $itemObject->getVar($this->item_adult_fld) ? "[ADULT]<br />" : "" ;
			$detail4html .= _MD_XMOBILE_TITLE . " : ";
			$detail4html .= $adult . $title.'<br />';
		}
		// USER NAME
		if (!is_null($this->item_uid_fld))
		{
			$uid = $itemObject->getVar($this->item_uid_fld);
			$uname = $this->getUserLink($uid);
			$detail4html .= _MD_XMOBILE_CONTRIBUTOR.$uname.'<br />';
		}
		// DATE
		if (!is_null($this->item_date_fld))
		{
			$date = $itemObject->getVar($this->item_date_fld);
			$detail4html .= _MD_XMOBILE_DATE.$this->utils->getDateLong($date).'<br />';
			$detail4html .= _MD_XMOBILE_TIME.strftime('%H:%M',$date).'<br />';
		}
		// VISIT
		if (!is_null($this->item_hits_fld))
		{
			$detail4html .= _MD_XMOBILE_HITS.$itemObject->getVar($this->item_hits_fld).'<br />';
		}
		// DESC
		$linksDescHandler =& new XmobileLinksDescHandler( $this->itemTextName , $this->db );
		$linksDescObject = $linksDescHandler->get( $this->item_id );
		if(is_object($linksDescObject))
		{
			$description = strip_tags($linksDescObject->getVar('description'));
			$len = 70 ;
			if ( function_exists('mb_strlen') && function_exists('mb_substr') ) {
				if( mb_strlen($description) > $len ){
					$description = mb_substr($description,0,$len)."..";
				}
			}elseif( strlen($description) > $len ){
				$description = substr($description,0,$len)."..";
			}
			if($description != '')
			{
				$detail4html .= _MD_XMOBILE_CONTENTS.'<br />';
				$detail4html .= $description.'<br />';
			}
		}
		
		require_once dirname(dirname(__FILE__)) .'/include/functions.php' ;
		// murl
		if (!is_null($this->item_murl_fld))
		{
			$murl = $itemObject->getVar($this->item_murl_fld) ;
			if( !empty($murl) ){
				$murl = links_urlCheckReplace( $murl ) ;
				$detail4html .= "<a href='$murl'>- MOBILE LINK - </a><br />";
			}
		}
		// url
		if (!is_null($this->item_url_fld))
		{
			$url = $itemObject->getVar($this->item_url_fld) ;
			if( !empty($url) ){
				$url = links_urlCheckReplace( $url ) ;
				$detail4html .= "PC: $url<br />";
			}
		}

		return $detail4html;
	}

}

/////////////////////////////////////////////////////////////////////////////////////////
class XmobileLinksDesc extends XmobileTableObject
{
	function XmobileLinksDesc()
	{
		$this->initVar('lid', XOBJ_DTYPE_INT, '0', true);
		$this->initVar('description', XOBJ_DTYPE_TXTAREA, '', true);
		$this->setKeyFields(array('lid'));
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
class XmobileLinksDescHandler extends XmobileTableObjectHandler
{
	function XmobileLinksDescHandler( $table_text , $db )
	{
		XmobileTableObjectHandler::XmobileTableObjectHandler($db);
		$this->tableName = $this->db->prefix($table_text);
	}
}
